package com.cdtu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdtu.entity.Speciality;
import com.cdtu.entity.SpecialityType;
import com.cdtu.service.SpecialityService;
import com.cdtu.service.SpecialityTypeService;
import com.cdtu.vo.R;
import com.cdtu.vo.SpecialityTypeVO;
import com.cdtu.vo.SpecialityVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Sakura
 * @description 针对表【certify_type】的数据库操作Mapper
 * @createDate 2023-01-13 15:17:24
 * @Entity com.cdtu.entity.specialityType
 */

@Slf4j
//解决跨域问题
@CrossOrigin
@RequestMapping("/specType")
@RestController
public class SpecialityTypeController {

    @Autowired
    private SpecialityService specialityService;

    @Autowired
    private SpecialityTypeService specialityTypeService;
    /**
     *这个方法获取的是对象，不是普通的字符串，给添加和修改用户时候选择技能特长用的
     * @param :
     * @return R
     * @author Sakura
     * @date 2023/1/16 12:10
     */
    @GetMapping("/getAll")
    private R typeAndSpec(){

//        首先需要特长id和value，然后
        List<SpecialityType> specialityTypes = specialityTypeService.list();

        //最外层技能特长类型传回集合
        List<SpecialityTypeVO> specialityTypeVOS=new ArrayList<>();

        //所有的类型集合遍历
        for (SpecialityType specialityType : specialityTypes) {
            Long typeId = specialityType.getId();

            //每一类型都有很多技能特长的对象，所以循环的时候需要创建tvo对象
            SpecialityTypeVO tvo=new SpecialityTypeVO();

            tvo.setIndex(typeId-1);
//            tvo.setId(typeId);
            tvo.setValue(specialityType.getName());
//            tvo.setType(specialityType.getName());

            //第二层技能特长集合
            List<SpecialityVO> specialityVOS=new ArrayList<>();

            //根据id查询所有技能特长的证书,封装到集合中
//            LambdaQueryWrapper<Speciality> queryWrapper=new LambdaQueryWrapper<>();
//            queryWrapper.eq(Speciality::getId,typeId);
            List<Speciality> specialityList = specialityService.findByTypeId(typeId);

            //将技能特长集合封装成对象
            for (Speciality speciality : specialityList) {
                SpecialityVO svo=new SpecialityVO();
                svo.setValue(speciality.getName());
                specialityVOS.add(svo);
            }

            //技能特长查询完毕之后需要将技能特长封装到对象中
            tvo.setSpeciality(specialityVOS);

//            将封装好的数据添加到第一层集合中
            specialityTypeVOS.add(tvo);
        }

        for (SpecialityTypeVO specialityTypeVO : specialityTypeVOS) {
            System.out.println(specialityTypeVO);
        }
        return R.success(specialityTypeVOS);
    }


}